<template>
  <div class="base_home">
    <el-container>
      <!--用户资料区域-->
      <el-header>
        <div class="left-header">
          <el-badge is-dot class="item">
            <div class="user_header">
              <img width="100%" height="100%" :src="user.avatar"/>
            </div>
          </el-badge>
          <div class="user_info">
            {{user.username}}
          </div>
        </div>
        <div class="middle-header">{{activeName}}</div>
        <div class="right-header">
          <div class="right-icon">
            <i class="el-icon-s-opportunity"/>
          </div>
        </div>
      </el-header>
      <!--路由主体区域-->
      <el-main>
        <router-view/>
      </el-main>
      <!--菜单导航区域-->
      <el-footer>
        <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          menu-trigger="click"
          :router="true"
          @select="handleSelect"
          background-color="#fff"
          text-color="#909399"
          active-text-color="#fff">
          <el-menu-item v-for="item in user.mavList" :key="item.id" :index="item.routerUrl">
            <el-badge is-dot class="item">
              <i :class="item.icon"/>
            </el-badge>
            {{item.menuName}}
          </el-menu-item>
        </el-menu>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
// import { Enc } from '../utils/secret'

export default {
  name: 'Home',
  data () {
    return {
      activeIndex: '/home/index',
      activeName: '首页',
      user: {
        username: '逆风寻你',
        avatar: '/wyserver/file/sftp/show/6625da184ca110e9ac98bf8f9a1d2c9d',
        mavList: []
      }
    }
  },
  created () {
    this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    async getUserInfo () {
      try {
        // const ui = JSON.parse(localStorage.getItem('userInfo'))
        // if (!ui) {
        const { data: res } = await this.$http.post('/ajshow/user/info')
        if (!res.status) return this.$message.error(res.msg ? res.msg : '获取用户信息异常')
        // console.log(res.data)
        this.user.username = res.data.nickname
        this.user.avatar = '/wyserver/file/sftp/show/' + res.data.avatar
        this.user.mavList = res.data.navs
        // localStorage.setItem('userInfo', JSON.stringify(this.user))
        // } else this.user = ui
        const ac = JSON.parse(localStorage.getItem('active'))
        if (!ac) {
          localStorage.setItem('active', JSON.stringify({ activeIndex: this.activeIndex, activeName: this.activeName }))
        } else {
          this.activeName = ac.activeName
          this.activeIndex = ac.activeIndex
        }
      } catch (e) {
        this.$message.error(e.toString())
      }
    },
    handleSelect (key, keyPath) {
      this.user.mavList.forEach((v, i, a) => {
        if (v.routerUrl === key) {
          this.activeIndex = v.routerUrl
          this.activeName = v.menuName
          localStorage.setItem('active', JSON.stringify({ activeIndex: this.activeIndex, activeName: this.activeName }))
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .base_home,.el-container{
    width: 100%;
    height: 100%;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    padding-top: 30px;
    background-color: #B3C0D1;
    height: 70px !important;
    .left-header{
      width: 40%;
      display: flex;
      .user_header{
        height: 30px;
        width: 30px;
        border-radius: 50%;

        overflow: hidden;
        line-height: 30px;
        // color: white;
        font-size: 30px;
        // background-color: #42CB8A;
      }
      .user_info{
        padding-left: 10px;
        font-size: 16px;
        color: white;
        white-space:nowrap;
        overflow:hidden;
        text-overflow:ellipsis;
      }
    }
    .middle-header{
      width: 20%;
      font-size: 20px;
      color: white;
      text-align: center;
    }
    .right-header{
      width: 40%;
      display: flex;
      justify-content: flex-end;
      .right-icon{
        color: white;
        font-size: 24px;
        height: 80%;
      }
    }
  }
  .el-main {
     /*background-color: #E9EEF3;*/
     /*color: #333;*/
     /*text-align: center;*/
     /*line-height: 160px;*/
    padding: 0;
   }
  .el-footer {
    padding: 0;
    background-color: #B3C0D1;
  }
  // 样式穿透 ::v-deep
  ::v-deep .el-menu {
    background-color: transparent !important;
    display: flex;
    justify-content: space-around;
    .el-menu-item{
      background-color: transparent !important;
      line-height: 25px;
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      align-items: center;
      white-space:nowrap;
      overflow:hidden;
      text-overflow:ellipsis;
      i{
        margin: 0;
      }
    }
    .el-menu-item:hover,.el-menu-item:focus{
      background-color: transparent !important;
    }
  }
</style>
